package dictionary

import (
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server/app/dao/dictionary"
	"gitee.com/baal1990/gitee.com/baal1990/cmd/server/app/global"
	"gitee.com/baal1990/gitee.com/baal1990/pkg/model"
	"github.com/gin-gonic/gin"
	"github.com/gin-gonic/gin/binding"
	"net/http"
	"time"
)

/*
*
requst param 获取数据字典
*/
type GetDictionarySetParam struct {
	DictName string `form:"dictName"`
	DictType string `form:"dictType"`
	Status   int    `form:"status"`
	PageSize int    `form:"pageSize" binding:"required"`
	CurPage  int    `form:"page" binding:"required"`
}

// 获取字典列表
func GetDictList(c *gin.Context) {
	reqParam := GetDictionarySetParam{}
	if err := c.BindWith(&reqParam, binding.Form); err != nil {
		global.Log.Sugar().Error("参数错误", err)
		c.JSON(http.StatusOK, model.Fail("参数错误"))
		return
	}
	if reqParam.PageSize < 0 || reqParam.CurPage < 0 {
		global.Log.Sugar().Error("页码值非法")
		c.JSON(http.StatusOK, model.Fail("页码值非法，只可为正数"))
		return
	}
	count, listValue, err := dictionary.GetDictList(reqParam.DictName,
		reqParam.DictType,
		reqParam.Status,
		reqParam.PageSize,
		reqParam.CurPage)

	if err != nil {
		global.Log.Sugar().Error("获取数据字典失败", err)
		c.JSON(http.StatusOK, err)
		return
	}
	c.JSON(http.StatusOK, model.SuccessGrid(count, listValue))
}

/*
*
requst param 插入或更新数据字典
*/
type PutDictionarySetParam struct {
	Id      int64  `form:"Id"`
	Name    string `form:"Name" binding:"required"`
	Type    string `form:"Type" binding:"required"`
	Isused  int    `form:"Isused"`
	Remarks string `form:"Remarks"`
}

// 插入或更新数据字典
func PutDict(c *gin.Context) {
	reqParam := PutDictionarySetParam{}
	if err := c.BindWith(&reqParam, binding.Form); err != nil {
		global.Log.Sugar().Error("设置数据字典失败", err)
		c.JSON(http.StatusOK, model.Fail("参数错误"))
		return
	}
	if reqParam.Isused != dictionary.ENABLED && reqParam.Isused != dictionary.DISABLED {
		global.Log.Sugar().Error("Isused值非法")
		c.JSON(http.StatusOK, model.Fail("Isused值非法，只可取0或1"))
		return
	}
	dict := dictionary.DictionarySet{
		Id:        reqParam.Id,
		Name:      reqParam.Name,
		Type:      reqParam.Type,
		CreatedTs: time.Now(),
		Remarks:   reqParam.Remarks,
		Isused:    reqParam.Isused,
	}
	if err := dictionary.SetDict(&dict); err != nil {
		global.Log.Sugar().Error("插入或更新数据字典失败", err)
		c.JSON(http.StatusOK, model.Fail("插入或更新数据字典失败"))
		return
	}
	c.JSON(http.StatusOK, model.Success("操作成功"))

}

/*
	删除数据字典
*/

type DelDictionarySetParam struct {
	Id int64 `form:"DictId" binding:"required"`
}

func DelDict(c *gin.Context) {
	reqParam := DelDictionarySetParam{}
	if err := c.BindWith(&reqParam, binding.Form); err != nil {
		global.Log.Sugar().Error("删除数据字典失败", err)
		c.JSON(http.StatusOK, model.Fail("参数错误"))
		return
	}
	if err := dictionary.DelDict(reqParam.Id); err != nil {
		global.Log.Sugar().Error("删除数据字典失败", err)
		c.JSON(http.StatusOK, model.Fail("删除字典失败"))
		return
	}
	c.JSON(http.StatusOK, model.Success("操作成功"))
}

/*
重置字典状态
*/
type putDictionaryStatusParam struct {
	Id     int64 `form:"id" binding:"required"`
	Status *int  `form:"isused" binding:"required"`
}

func PutDictStatus(c *gin.Context) {
	reqParam := putDictionaryStatusParam{}
	if err := c.BindWith(&reqParam, binding.Form); err != nil {
		global.Log.Sugar().Error("设置字典状态失败", err)
		c.JSON(http.StatusOK, model.Fail("参数错误"))
		return
	}
	if *reqParam.Status != dictionary.ENABLED && *reqParam.Status != dictionary.DISABLED {
		global.Log.Sugar().Error("Isused值非法")
		c.JSON(http.StatusOK, model.Fail("Isused值非法，只可取0或1"))
		return
	}
	if err := dictionary.SetDictStatus(reqParam.Id, *reqParam.Status); err != nil {
		global.Log.Sugar().Error("设置字典状态失败", err)
		c.JSON(http.StatusOK, model.Fail("设置字典状态失败"))
		return
	}
	c.JSON(http.StatusOK, model.Success("操作成功"))
}

func PutDictDetailStatus(c *gin.Context) {
	reqParam := putDictionaryStatusParam{}
	if err := c.BindWith(&reqParam, binding.Form); err != nil {
		global.Log.Sugar().Error("设置字典状态失败", err)
		c.JSON(http.StatusOK, model.Fail("参数错误"))
		return
	}
	if *reqParam.Status != dictionary.ENABLED && *reqParam.Status != dictionary.DISABLED {
		global.Log.Sugar().Error("Isused值非法")
		c.JSON(http.StatusOK, model.Fail("Isused值非法，只可取0或1"))
		return
	}
	if err := dictionary.SetDictDetailStatus(reqParam.Id, *reqParam.Status); err != nil {
		global.Log.Sugar().Error("设置字典状态失败", err)
		c.JSON(http.StatusOK, model.Fail("设置字典状态失败"))
		return
	}
	c.JSON(http.StatusOK, model.Success("操作成功"))
}

/*
根据ID查字典详情
*/
type getDictionaryDetailParam struct {
	Id int64 `form:"dictid" binding:"required"` //Id 一定要大写
}

func GetDictDetail(c *gin.Context) {
	reqParam := getDictionaryDetailParam{}
	if err := c.BindWith(&reqParam, binding.Form); err != nil {
		global.Log.Sugar().Error("查询字典详情失败", err)
		c.JSON(http.StatusOK, model.Fail("参数错误"))
		return
	}
	list, err := dictionary.GetDictDetailList(reqParam.Id)
	if err != nil {
		global.Log.Sugar().Error("查询字典详情失败", err)
		c.JSON(http.StatusOK, model.Fail("查询字典详情失败"))
		return
	}
	c.JSON(http.StatusOK, model.SuccessObject("操作成功", list))
}

/*
requst param 插入或更新数据字典字典
*/
type PutDictionaryDetailParam struct {
	Id      int64  `form:"id"`
	Key     string `form:"key" binding:"required"`
	Value   string `form:"value" binding:"required"`
	Isused  int    `form:"isused"`
	Remarks string `form:"remarks"`
	DictId  int    `form:"dictid"`
}

// 插入或更新数据字典字典
func PutDictDetail(c *gin.Context) {
	reqParam := PutDictionaryDetailParam{}
	if err := c.BindWith(&reqParam, binding.Form); err != nil {
		global.Log.Sugar().Error("设置数据字典详情失败", err)
		c.JSON(http.StatusOK, model.Fail("参数错误"))
		return
	}
	if reqParam.Isused != dictionary.ENABLED && reqParam.Isused != dictionary.DISABLED {
		global.Log.Sugar().Error("Isused值非法")
		c.JSON(http.StatusOK, model.Fail("Isused值非法，只可取0或1"))
		return
	}
	dict := dictionary.DictionaryDetail{
		Id:        reqParam.Id,
		Key:       reqParam.Key,
		Value:     reqParam.Value,
		Remarks:   reqParam.Remarks,
		Isused:    reqParam.Isused,
		CreatedTs: time.Now(),
		DictId:    reqParam.DictId,
	}
	if err := dictionary.SetDictDetail(&dict); err != nil {
		global.Log.Sugar().Error("插入或更新数据字典详情失败", err)
		c.JSON(http.StatusOK, model.Fail("插入或更新数据字典详情失败"))
		return
	}
	c.JSON(http.StatusOK, model.Success("操作成功"))
}

/*
	删除数据字典详细
*/

type DelDictionaryDetailParam struct {
	Id int64 `form:"id" binding:"required"`
}

// 插入或更新数据字典字典
func DelDictDetail(c *gin.Context) {
	reqParam := DelDictionaryDetailParam{}
	if err := c.BindWith(&reqParam, binding.Form); err != nil {
		global.Log.Sugar().Error("删除数据字典详情失败", err)
		c.JSON(http.StatusOK, model.Fail("参数错误"))
		return
	}

	if err := dictionary.DelDictDetail(reqParam.Id); err != nil {
		global.Log.Sugar().Error("删除数据字典详情失败", err)
		c.JSON(http.StatusOK, model.Fail("删除数据字典详情失败"))
		return
	}
	c.JSON(http.StatusOK, model.Success("操作成功"))
}

// 查询字典所有信息
func GetDictInfo(c *gin.Context) {
	info, err := dictionary.GetDictInfo()
	if err != nil {
		global.Log.Sugar().Error("获取数据字典失败", err)
		c.JSON(http.StatusOK, model.Fail("获取数据字典失败"))
		return
	}
	c.JSON(http.StatusOK, model.SuccessObject("操作成功", info))
}

/*
*

	查询字典字典所有信息by name
*/
type GetDictionaryDetailParam struct {
	Name string `form:"name" binding:"required"`
}

func GetDictDetailByName(c *gin.Context) {
	reqParam := GetDictionaryDetailParam{}
	if err := c.BindWith(&reqParam, binding.Form); err != nil {
		global.Log.Sugar().Error("获取数据字典详情失败", err)
		c.JSON(http.StatusOK, model.Fail("参数错误"))
		return
	}
	info, err := dictionary.GetDictDetailListByName(reqParam.Name)
	if err != nil {
		global.Log.Sugar().Error("获取数据字典详情失败", err)
		c.JSON(http.StatusOK, model.Fail("获取数据字典详情失败"))
		return
	}
	c.JSON(http.StatusOK, model.SuccessObject("操作成功", info))
}
